﻿using CommunityToolkit.Mvvm.Input;
using ICSharpCode.AvalonEdit;
using System.IO;
using System.Windows.Input;
using VideoTools.Extension;
using VideoTools.Util;
using VideoTools.vo;

namespace VideoTools.Commands
{
    
    public class MargeSubtitleCommands : NotifyPropertyBase
    {
        private static volatile MargeSubtitleCommands _instance;
        private static readonly object SyncRoot = new object();

        public static MargeSubtitleCommands Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (SyncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new MargeSubtitleCommands();
                        }
                    }
                }

                return _instance;
            }
        }

        private MargeSubtitleCommands() { }

       
        private string _Subtitle1;

        public string Subtitle1
        {
            get => _Subtitle1;
            set => SetProperty(ref _Subtitle1, value);
        }
        private string _Subtitle2;

        public string Subtitle2
        {
            get => _Subtitle2;
            set => SetProperty(ref _Subtitle2, value);
        }

        private string _SubtitleFile1;

        public string SubtitleFile1
        {
            get => _SubtitleFile1;
            set => SetProperty(ref _SubtitleFile1, value);
        }
        private string _SubtitleFile2;

        public string SubtitleFile2
        {
            get => _SubtitleFile2;
            set => SetProperty(ref _SubtitleFile2, value);
        }
        private static readonly string autoMergeEndWith = "__合并__timeline_out";
        private string srtFileExt = "";
        private ICommand _CheckSubtitleFileCommand;
        private ICommand _OpenSubtitleFileCommand;
        private ICommand _MargeSubtitleFileCommand;

        public ICommand CheckSubtitleFileCommand =>
            _CheckSubtitleFileCommand
            ?? (
                _CheckSubtitleFileCommand = new RelayCommand<object>(o =>
                {
                    TextEditor te = o as TextEditor;
                    string name = te.Name.Replace("SubtitleEditer", "");

                    bool check = SubtitleUtils.checkSub(te);
                    if (!check)
                    {
                        return;
                    }
                    if (name.Contains("1"))
                    {
                        te.Save(SubtitleFile1);
                    }
                    else
                    {
                        te.Save(SubtitleFile2);
                    }
                })
            );

      
        public ICommand OpenSubtitleFileCommand =>
            _OpenSubtitleFileCommand
            ?? (
                _OpenSubtitleFileCommand = new RelayCommand<object>(o =>
                {
                    OpenFileDialog openFileDialog = new OpenFileDialog();
                    openFileDialog.Filter = "字幕文件(*.srt)|*.srt";
                    openFileDialog.RestoreDirectory = true;
                    openFileDialog.FilterIndex = 1;
                    TextEditor te = o as TextEditor;
                    string name = te.Name.Replace("SubtitleEditer", "");

                    if (name.Contains("1"))
                    {
                        if (openFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            SubtitleFile1 = openFileDialog.FileName;
                            srtFileExt = System.IO.Path.GetExtension(SubtitleFile1);
                            var fileContent = Utils.ReadFile(SubtitleFile1);
                            Subtitle1 = fileContent.content;
                            te.Text = Subtitle1;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        if (openFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            SubtitleFile2 = openFileDialog.FileName;
                            var fileContent = Utils.ReadFile(SubtitleFile2);
                            srtFileExt = System.IO.Path.GetExtension(SubtitleFile1);

                            Subtitle2 = fileContent.content;
                            te.Text = Subtitle2;
                        }
                        else
                        {
                            return;
                        }
                    }
                })
            );
        
        public ICommand MargeSubtitleFileCommand =>
            _MargeSubtitleFileCommand
            ?? (
                _MargeSubtitleFileCommand = new RelayCommand<object>(o =>
                {
                    var controlArray = (object[])o;

                    TextEditor te1 = (TextEditor)controlArray[0];
                    TextEditor te2 = (TextEditor)controlArray[1];
                    List<string> lines1 = te1.GetLines();
                    List<string> lines2 = te2.GetLines();
                    var txts1Obj = SubtitleUtils.ReadSubtitle(lines1);
                    var txts2Obj = SubtitleUtils.ReadSubtitle(lines2);
                    bool hasHz1 = txts1Obj.hasHz;

                    List<Subtitle> mergeList = hasHz1 ? SubtitleUtils.MergeSubtitle(txts1Obj.list, txts2Obj.list) :
                        SubtitleUtils.MergeSubtitle(txts2Obj.list, txts1Obj.list);
                    string name2 = System.IO.Path.GetFileName(SubtitleFile2);
                    name2 = name2.Replace(srtFileExt, "");
                    string filePath = SubtitleFile1.Replace(srtFileExt, "-" + name2 + autoMergeEndWith + srtFileExt);
                    using (StreamWriter writer = new StreamWriter(filePath))
                    {
                        foreach (Subtitle subtitle in mergeList)
                        {
                            writer.WriteLine(subtitle.Number);
                            writer.WriteLine($"{subtitle.StartTime:hh\\:mm\\:ss\\,fff} --> {subtitle.EndTime:hh\\:mm\\:ss\\,fff}");
                            writer.WriteLine(subtitle.Text);
                            writer.WriteLine();
                        }
                    }
                })
            );
    }
}
